from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_plant.entity.do.plot_do import PlantPlot
from module_plant.entity.vo.plot_vo import PlotModel, PlotPageQueryModel
from utils.page_util import PageUtil


class PlotDao:
    """
    种植地块模块数据库操作层
    """

    @classmethod
    async def get_plot_detail_by_id(cls, db: AsyncSession, plot_id: int):
        """
        根据地块Id获取种植地块详细信息

        :param db: orm对象
        :param plot_id: 地块Id
        :return: 种植地块信息对象
        """
        plot_info = (
            (
                await db.execute(
                    select(PlantPlot)
                    .where(
                        PlantPlot.plot_id == plot_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return plot_info

    @classmethod
    async def get_plot_detail_by_info(cls, db: AsyncSession, plot: PlotModel):
        """
        根据种植地块参数获取种植地块信息

        :param db: orm对象
        :param plot: 种植地块参数对象
        :return: 种植地块信息对象
        """
        plot_info = (
            (
                await db.execute(
                    select(PlantPlot).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return plot_info

    @classmethod
    async def get_plot_list(cls, db: AsyncSession, query_object: PlotPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取种植地块列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 种植地块列表信息对象
        """
        query = (
            select(PlantPlot)
            .where(
                PlantPlot.plot_name.like(f'%{query_object.plot_name}%') if query_object.plot_name else True,
                PlantPlot.plot_no == query_object.plot_no if query_object.plot_no else True,
                PlantPlot.year == query_object.year if query_object.year else True,
                PlantPlot.product_area_id == query_object.product_area_id if query_object.product_area_id else True,
                PlantPlot.planter_id == query_object.planter_id if query_object.planter_id else True,
            )
            .order_by(PlantPlot.plot_id)
            .distinct()
        )
        plot_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return plot_list

    @classmethod
    async def add_plot_dao(cls, db: AsyncSession, plot: PlotModel):
        """
        新增种植地块数据库操作

        :param db: orm对象
        :param plot: 种植地块对象
        :return:
        """
        db_plot = PlantPlot(**plot.model_dump(exclude={}))
        db.add(db_plot)
        await db.flush()

        return db_plot

    @classmethod
    async def edit_plot_dao(cls, db: AsyncSession, plot: dict):
        """
        编辑种植地块数据库操作

        :param db: orm对象
        :param plot: 需要更新的种植地块字典
        :return:
        """
        await db.execute(update(PlantPlot), [plot])

    @classmethod
    async def delete_plot_dao(cls, db: AsyncSession, plot: PlotModel):
        """
        删除种植地块数据库操作

        :param db: orm对象
        :param plot: 种植地块对象
        :return:
        """
        await db.execute(delete(PlantPlot).where(PlantPlot.plot_id.in_([plot.plot_id])))

